<?php
namespace app\api\service;
use think\Controller;
use think\Db;

class Message extends Controller {
    static protected $err_code = 0;
    // 获取未读消息数量
    static public function get_msg_unread_count($param) {
        if(!isset($param['member_id']) || !isset($param['member_role']) || !isset($param['terrace']))
            return ['data'=>'','code'=>400001,'msg'=> 'FAULT'];

        try{
            $where = [
                'member_id' => $param['member_id'],
                'member_role' => $param['member_role'],
                'terrace' => $param['terrace'],
                'status' => 0
            ];
            // 系统消息
            $where['msg_type'] = 1;
            $sys_count = Db::table('mob_member_message')->where($where)->count();
            $sys_time = Db::table('mob_member_message')->where($where)->order('id desc')->value('add_time');
            // 订单消息
            $where['msg_type'] = 2;
            $order_count = Db::table('mob_member_message')->where($where)->count();
            $order_time = Db::table('mob_member_message')->where($where)->order('id desc')->value('add_time');
            // 活动消息
            $where['msg_type'] = 3;
            $ac_count = Db::table('mob_member_message')->where($where)->count();
            //$ac_time = Db::table('mob_member_message')->where($where)->order('id desc')->value('add_time');
//            // 普通消息
//            $msg_count = Db::table('mob_member_message')->where('member_id', $param['member_id'])->where('type', 0)->where('status', 1)->count();

            return ['data'=>[
                'count' => $sys_count + $order_count + $ac_count,
                'order_count' => $order_count,
                'sys_count' => $sys_count,
                'sys_time' => $sys_time,
                'order_time' => $order_time
            ],'code'=>200,'msg'=> 'SUCCESS'];
        } catch (\Exception $e) {
            return ['code'=>self::$err_code, 'msg'=> $e->getMessage()];
        }
    }


    // 获取消息列表
    static public function get_msg_list($param){
        if( !isset($param['member_id']) || !isset($param['msg_type']) || !isset($param['member_role']) || !isset($param['terrace']))
            return ['data'=>'','code'=>400001,'msg'=> 'FAULT'];
        try{
            $where['mm.member_id'] = $param['member_id'];
            $where['mm.msg_type'] = $param['msg_type'];
            $where['mm.member_role'] = $param['member_role'];
            $where['mm.terrace'] = $param['terrace'];
            $member_message_list = Db::table('mob_member_message')->alias('mm')->field('me.*, mm.status, mm.add_time, mm.id as member_message_id')
                ->join('mob_message me', 'me.id = mm.msg_id')
                ->where($where)->order('status asc, add_time desc')->paginate();
            return ['data'=>$member_message_list, 'code'=>200, 'msg'=> 'SUCCESS'];
        } catch (\Exception $e) {
            return ['code'=>self::$err_code, 'msg'=> $e->getMessage()];
        }
    }

    // 获取消息详情
    static public function get_msg_info($param){
        if( !isset($param['msg_id']) || !isset($param['member_id'])  ) return ['data'=>'','code'=>400001,'msg'=> 'FAULT'];
        try{
            $where['mm.member_id'] = $param['member_id'];
            $where['mm.id'] = $param['msg_id'];
            $member_message_info = Db::table('mob_member_message')->alias('mm')->field('me.*, mm.add_time, mm.id as member_message_id, mm.msg_params')
                ->join('mob_message me', 'me.id = mm.msg_id')
                ->where($where)->find();
            //dump($member_message_info);
            if ($member_message_info['msg_params']) {
                $msg_params = json_decode($member_message_info['msg_params'], true);
                if ($msg_params['url']) {
                    $member_message_info['url'] = $msg_params['url'];
                    unset($msg_params['url']);
                }
                $member_message_info['message'] = vsprintf($member_message_info['message'], $msg_params);
            }
            return ['data'=>$member_message_info, 'code'=>200, 'msg'=> 'SUCCESS'];
        } catch (\Exception $e) {
            return ['code'=>self::$err_code, 'msg'=> $e->getMessage()];
        }
    }

    // 修改消息状态
    static public function update_msg_status($param){
        if( !isset($param['msg_id']) || !isset($param['member_id']) ) return ['data'=>'','code'=>400001,'msg'=> 'FAULT'];
        try{
            $where['member_id'] = $param['member_id'];
            $where['id'] = $param['msg_id'];
            Db::table('mob_member_message')->where($where)->update(['status' => 1]);
            return [ 'code'=>200, 'msg'=> 'SUCCESS'];
        } catch (\Exception $e) {
            return ['code'=>self::$err_code, 'msg'=> $e->getMessage()];
        }
    }

    /************************************
    *
     * 生成订单消息
     * param member_id 接受者ID
     * param member_role 接受者角色类型
     * param message 消息内容
     * param title 消息标题
     * param url 消息跳转连接
     ******/
    static public function creation_order_msg($param, $data) {
        if(!isset($param['member_id']) || !isset($param['member_role']) || !isset($param['msg_id']))
            return ['data'=>'','code'=>400001,'msg'=> 'FAULT'];

        // 启动事务
        Db::startTrans();
        try {
            // 推送给用户--用户全平台可读  需要给用户推送四条消息
            $terraces = ['ios', 'android', 'pc', 'weixin'];
            $member_smg = [];
            foreach ($terraces as $v) {
                $member_smg[] = [
                    'member_id' => $param['member_id'],
                    'member_role' => $param['member_role'],
                    'terrace' => $v,
                    'msg_id' => $param['msg_id'],
                    'add_time' => time(),
                    'msg_type' => 2,
                    'msg_params' => json_encode($data),
                ];
            }
            Db::table('mob_member_message')->insertAll($member_smg);
            Db::commit();
            return ['code' => 200, 'msg' => 'SUCCESS'];
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return ['code' => self::$err_code, 'msg' => $e->getMessage()];
        }
    }
}





